package Visibility;

import java.util.Arrays;
import java.util.PriorityQueue;
import java.util.concurrent.locks.ReentrantLock;

public class TestLockHappensBefore {
    static int a = 0;
    static ReentrantLock lock = new ReentrantLock();

    public static void modify1() {
        lock.lock();
        try {
            System.out.println("a= " + a + " with " + Thread.currentThread().getName());
        } finally {
            a = 5;
            lock.unlock();
        }
    }

    public static void modify2() {
        lock.lock();
        try {
            System.out.println("a= " + a + " with " + Thread.currentThread().getName());
        } finally {
            a = 10;
            lock.unlock();
        }
    }

    public static void main(String[] args) {
        Thread thread0 = new Thread() {
            @Override
            public void run() {
                modify2();
            }
        };
        Thread thread1 = new Thread() {
            @Override
            public void run() {
                modify1();
            }
        };

        thread1.start();
        thread0.start();

    }
}
